/**
 * 详情对话框
 */
var StoragePutSingleInfoDlg = {
    data: {
    	positionNumber: "",
    }
};
/**
 * 添加或者修改页面
 */
var OrderPutDetailedInfoDlg = {
    data: {
        detailedGoodsname: "",
        detailedGoodsnum: "",
        goodsNumber: "",
		goodsNo:"",
        detailedRebubbles: "",
        detailedPacking: "",
        detailedLargeclass: "",
        detailedGoodstype: "",
        detailedPutcondition: "",
        detailedPiecenum: "",
        goodsWeight: "",
        goodsVolume: "",
    }
};
layui.use(['table', 'admin', 'ax','laydate','form','element'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;
	var element = layui.element
	
    laydate.render({
      elem: '#startTime3'
    });

    laydate.render({
      elem: '#endTime3'
    });
	
	//tab切换
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})
	
    /**
     * 库存表管理
     */
    var StorageStock = {
        tableId: "storageStockTable"
    };
	
    /**
     * 库存表管理
     */
    var StorageStock2 = {
        tableId: "storageStockTable2"
    };

    /**
     * 库存流水管理
     */
    var StorageStockDetailed = {
        tableId: "storageStockDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    StorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'stockstate', title: 'Status',templet: '#stockstateTpl'},
            {field: 'operateName', title: 'Project'},
            {field: 'customerName', title: 'Customer'},
            {field: 'warehouseName', title: 'Warehouse'},
            {field: 'goodsName', title: 'Product', style:'background-color: #dbf1e1;font-weight: 600;',width:270},
            {field: 'goodsNumber', title: 'Code', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', title: 'SKU', sort: true, style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: 'Total:', event: 'checkbox'},
            {field: 'num', title: 'Quantity', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            /*{field: 'storageNumber', title: '待拣货数', totalRow: true},*/
            {field: 'stockWeight', title: 'Weight', totalRow: true},
            {field: 'stockVolume', title: 'Volume', totalRow: true},
            {field: 'stockMoney', title: 'Amount', totalRow: true},
            {field: 'unitprice', title: 'Unit Price'},
            {field: 'goodsBigtype', title: 'Category'},
            {field: 'goodsType', title: 'Type', hide: true},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'goodsPacking', title: 'Packing'},
            {field: 'goodsSpecs', title: 'Specs'},
            {field: 'goodsWarranty', title: 'Shelf Life (days)'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStock.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['goodsNo'] = $("#goodsNo").val();
        table.reload(StorageStock.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    StorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStock.tableId,
        url: Feng.ctxPath + '/storageStock/stockListGoods',
        page: true,
        height: 400,
        limit: 60,
		limits:[60,120,240,360,480,600,1500,5000,10000],
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: StorageStock.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
    	StorageStock.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
    	StorageStock.exportExcel();
    });
    
    //点击选择商品名称
    $('#choiceg').click(function () {
    	var customerId = $("#customerId").val();
    	if (customerId==null||customerId=='') {
    		layer.msg('Please select customer'); 
		} else {
	        layer.open({
	            type: 2,
	            title: 'Select Product',
	            area: ['900px', '500px'],
	            content: Feng.ctxPath + '/orderPutDetailed/toChoiceGoods?goodsCustomerId=' + customerId,
	            end: function () {
	                $("#goodsName").val(OrderPutDetailedInfoDlg.data.detailedGoodsname);//商品名称
	                $("#goodsNumber").val(OrderPutDetailedInfoDlg.data.goodsNumber);//商品编号
	                $("#goodsNo").val(OrderPutDetailedInfoDlg.data.goodsNo);//货号
	            }
	        });
		}
    });
    
    //查询商品类别
    form.on('select(goodsBigtype)',function(){
    	$("#goodsType").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/storageStock/queryGoodsType',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"dictcode":$("#goodsBigtype").find("option:checked").attr("id"),"code":"COMMODITY_CATEGORY"},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>请选择</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.name+"'>"+value.name+"</option>";
    	        	});
    	        	$("#goodsType").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });

    /**
     * 初始化表格的列
     */
    StorageStock2.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'putTime', title: 'Receipt Batch'},
            {field: 'areaNumber', title: 'Area'},
            {field: 'positionNumber', title: 'Location', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'trayNumber', title: 'Tray', style:'background-color: #ecf5ff;font-weight: 600;', totalRowText: 'Total:'},
            {field: 'num', title: 'Quantity', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'storageNumber', title: 'Pending Qty', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'producetime', title: 'Production Date', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsbatchs', title: 'Batch', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'stockWeight', title: 'Weight', totalRow: true},
            {field: 'stockVolume', title: 'Volume', totalRow: true},
            {field: 'stockMoney', title: 'Amount', totalRow: true},
            {field: 'unitprice', title: 'Unit Price'}
        ]];
    };

    /**
     * 初始化表格的列
     */
    StorageStockDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID', style:'font-weight: 600;'},
            {field: 'operation', sort: true, title: 'Transaction Type', style:'font-weight: 600;'},
            {field: 'orderNumber', title: 'Order No.', style:'color: #2b85e4;font-weight: 600;',event: 'detailedSign'},
            {field: 'type', sort: true, title: 'Type', style:'font-weight: 600;'},
            {field: 'content', title: 'Content', style:'font-weight: 600;'},
            {field: 'warehouseName', sort: true, title: 'Warehouse', style:'font-weight: 600;'},
            {field: 'areaName', sort: true, title: 'Area', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: 'Location', style:'font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: 'Tray', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: 'Product', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Code', style:'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'SKU', style:'font-weight: 600;'},
            {field: 'goodsUnit', title: 'Unit', style:'font-weight: 600;'},
            {field: 'goodsPacking', title: 'Packing', style:'font-weight: 600;'},
            {field: 'goodsSpecs', title: 'Specs', style:'font-weight: 600;'},
            {field: 'putTime', sort: true, title: 'Receipt Batch', style:'font-weight: 600;'},
            {field: 'goodsbatchs', sort: true, title: 'Batch', totalRowText: 'Total:', style:'font-weight: 600;'},
            {field: 'num', sort: true, title: 'Updated Qty', totalRow: true, style:'font-weight: 600;'},
            {field: 'weight', sort: true, title: 'Weight', totalRow: true, style:'font-weight: 600;'},
            {field: 'volume', sort: true, title: 'Volume', totalRow: true, style:'font-weight: 600;'},
            {field: 'money', sort: true, title: 'Amount', totalRow: true, style:'font-weight: 600;'},
            {field: 'createTime', sort: true, title: 'Time', style:'font-weight: 600;'},
            {field: 'createName', sort: true, title: 'Operator', style:'font-weight: 600;'},
            {field: 'createAccount', sort: true, title: 'User', style:'font-weight: 600;'}
        ]]
    };

    // 单击行事件
    table.on('row(' + StorageStock.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + StorageStock.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StorageStock.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
        findtab(data);
    });

    var find = 0
    
	function findtab(data){
		$("#tabmsg").show()
    	$("#warehouseId").val(data.warehouseId)
    	$("#goodsId").val(data.goodsId)
    	find++
    	
        // 渲染商品明细表格
        var tableResult2 = table.render({
            elem: '#' + StorageStock2.tableId,
            url: Feng.ctxPath + '/storageStock/stockList?warehouseId=' + data.warehouseId+'&goodsId='+data.goodsId+'&stockstate=in_stock',
            page: true,
            limit: 60,
            height: 400,
			limits:[60,120,240,360,480,1500],
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: StorageStock2.initColumn()
        });

        // 渲染商品明细表格
        var tableResult3 = table.render({
            elem: '#' + StorageStockDetailed.tableId,
            url: Feng.ctxPath + '/storageStockDetailed/listGoods?warehouseId=' + data.warehouseId+'&goodsId='+data.goodsId,
            page: true,
            limit: 40,
            height: 400,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: StorageStockDetailed.initColumn()
        });

	    /**
	     * 点击查询按钮
	     */
	    StorageStock2.search = function () {
	        var queryData = {};
	        queryData['positionNumber'] = $("#positionNumber").val();
	        queryData['trayNumber'] = $("#trayNumber").val();
	        queryData['goodsbatchs'] = $("#goodsbatchs").val();
	        table.reload(StorageStock2.tableId, {
	            where: queryData, page: {curr: 1},
	             done:function () {
	            }
	        });
	    };

	    /**
	     * 导出excel按钮
	     */
	    StorageStock2.exportExcel = function () {
	        var checkRows = table.checkStatus(StorageStock2.tableId);
	        if (checkRows.data.length === 0) {
	            Feng.error("Please select data to export");
	        } else {
	            table.exportFile(tableResult2.config.id, checkRows.data, 'xls');
	        }
	    };
	    
	    /**
	     * 点击查询按钮
	     */
	    StorageStockDetailed.search = function () {
	        var queryData = {};
	        queryData['condition'] = $("#condition3").val();
	        queryData['startTime'] = $("#startTime3").val();
	        queryData['endTime'] = $("#endTime3").val();
	        table.reload(StorageStockDetailed.tableId, {
	            where: queryData, page: {curr: 1},
	             done:function () {
	            }
	        });
	    };

	    /**
	     * 导出excel按钮
	     */
	    StorageStockDetailed.exportExcel = function () {
	        var checkRows = table.checkStatus(StorageStockDetailed.tableId);
	        if (checkRows.data.length === 0) {
	            Feng.error("Please select data to export");
	        } else {
	            table.exportFile(tableResult3.config.id, checkRows.data, 'xls');
	        }
	    };
	    
	    if(find == 1){
		     // 搜索按钮点击事件
		     $('#btnSearch2').click(function () {
		    	 StorageStock2.search();
		     });
		     // 导出excel
		     $('#btnExp2').click(function () {
		    	 StorageStock2.exportExcel();
		     });
	
		    // 单击行事件
		    table.on('row(' + StorageStock2.tableId + ')', function (obj) {
		        var data = obj.data;
				$('div[lay-id="' + StorageStock2.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
				$('div[lay-id="' + StorageStock2.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
		    });
	
		     // 搜索按钮点击事件
		     $('#btnSearch3').click(function () {
		    	 StorageStockDetailed.search();
		     });
		     // 导出excel
		     $('#btnExp3').click(function () {
		    	 StorageStockDetailed.exportExcel();
		     });
	
		    // 单击行事件
		    table.on('row(' + StorageStockDetailed.tableId + ')', function (obj) {
		        var data = obj.data;
				$('div[lay-id="' + StorageStockDetailed.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
				$('div[lay-id="' + StorageStockDetailed.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
		    });
	
		    // 工具条点击事件
		    table.on('tool(' + StorageStockDetailed.tableId + ')', function (obj) {
		        var data = obj.data;
		        var layEvent = obj.event;
		
		        if (layEvent === 'detailedSign') {
					if(data.operation == 'Receipt'){
			            layer.open({
			                type: 2,
			                title: 'Receipt Details',
			                area: ['1050px', '500px'],
			                anim: 5,
			                content: Feng.ctxPath + '/storageStockDetailed/toPut?storageNumber=' + data.orderNumber,
			            });
					} else if(data.operation == 'Shipment'){
			            layer.open({
			                type: 2,
			                title: 'Shipment Details',
  			                area: ['1050px', '500px'],
			                anim: 5,
			                content: Feng.ctxPath + '/storageStockDetailed/toOut?storageOutNumber=' + data.orderNumber,
			            });
					}
		        }
		    });
	    }
	}
	

});
